package com.tanhua.manager.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.domain.vo.AnalysisUsersVo;
import com.tanhua.dubbo.api.db.AnalysisByDayApi;
import com.tanhua.dubbo.api.db.LogApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AnalysisByDayService {

    @DubboReference
    private LogApi logApi;

    @DubboReference
    private AnalysisByDayApi analysisByDayApi;

    public void analysis(String time) {
        logApi.analysis(time);
    }

    public ResponseEntity querySummary(String time) {
        AnalysisSummaryVo analysisSummaryVo = analysisByDayApi.querySummary(time);
        return ResponseEntity.ok(analysisSummaryVo);
    }

    public ResponseEntity queryUsers(Long sd, Long ed, Integer type) {
        String start = new DateTime(sd).toString("yyyy-MM-dd");
        String end = new DateTime(ed).toString("yyyy-MM-dd");
        List<AnalysisUsersVo> thisYearList = analysisByDayApi.queryUsersByYear(start, end, type);
        Date startDate = new Date(sd);
        DateTime startLastYear = DateUtil.offset(startDate, DateField.YEAR, -1);
        Date endDate = new Date(ed);
        DateTime endLastYear = DateUtil.offset(endDate, DateField.YEAR, -1);
        List<AnalysisUsersVo> lastYearList = analysisByDayApi.queryUsersByYear(startLastYear.toString("yyyy-MM-dd"),endLastYear.toString("yyyy-MM-dd"), type);
        Map map = new HashMap();
        map.put("thisYear", thisYearList);
        map.put("lastYear", lastYearList);
        return ResponseEntity.ok(map);
    }
}
